package cn.aran.org.controller;

import cn.aran.basic.annotation.PreAuthorize;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.org.domain.Department;
import cn.aran.org.query.DepartmentQuery;
import cn.aran.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 根据ID查询部门
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/24 10:02
    * @Parameters: [id]
    * @Return cn.aran.org.domain.Department
    */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Department loadById(@ApiParam(value="主键ID属性",required = true)  @PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询部门所有
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/24 11:02
    * @Parameters: []
    * @Return java.util.List<cn.aran.org.domain.Department>
    */
    @ApiOperation(value = "查询全部" )
    @GetMapping
    public List<Department> loadAll(){
        List<Department> departments = departmentService.loadAll();
        return departments;
    }

    /**
    * @Title: remove
    * @Description: 根据id删除部门
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/24 11:06
    * @Parameters: [id]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @ApiOperation(value = "部门删除" )
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门", sn = "department:delete")
    public AjaxResult remove(@ApiParam(value="主键ID属性",required = true)  @PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
    * @Title: patch
    * @Description: 部门批量删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/25 15:06
    * @Parameters: [id]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门", sn = "department:patch")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids
            );
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
    * @Title: saveOrUpt
    * @Description: 新增修改部门
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/24 11:50
    * @Parameters: [department]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @ApiOperation(value = "部门新增或修改" )
    @PutMapping
    @PreAuthorize(name = "保存部门", sn = "department:save")
    public AjaxResult saveOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }
    
    /**
    * @Title: queryData
    * @Description: 部门高级查询,分页
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/24 11:54  
    * @Parameters: [departmentQuery]
    * @Return cn.aran.basic.util.PageList<cn.aran.org.domain.Department>
    */
    @ApiOperation(value = "部门高级查询和分页" )
    @PostMapping
    @PreAuthorize(name = "列表部门", sn = "department:list")
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;
    }

    /**
    * @Title: loadDeptTree
    * @Description: 部门无限级树
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 10:21  
    * @Parameters: []
    * @Return java.util.List<cn.aran.org.domain.Department>
    */
    @GetMapping("/deptTree")
    public List<Department>  loadDeptTree(){
        return departmentService.loadDeptTree();
    }
}
